import XLSX from 'xlsx'

export default ({ name = 'excel', data = [], header = {} }) => {
  const cloneData = [...data]
  cloneData.unshift(header)
  const ws = XLSX.utils.json_to_sheet(
    cloneData,
    {
      header: Object.keys(header),
      skipHeader: true
    }
  )
  const wb = XLSX.utils.book_new()
  XLSX.utils.book_append_sheet(wb, ws, 'SheetJS')
  /* generate file and send to client */
  XLSX.writeFile(wb, `${name}.xlsx`)
}
